package com.ysm.smfu.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.adapter.CityAdapter;
import com.ysm.smfu.area.FileUtil;
import com.ysm.smfu.bean.City;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.QuickIndexBar;
import com.ysm.smfu.welcome.SharedConfig;
import com.zhy.autolayout.AutoLinearLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/23.
 */
public class LocationActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    @BindView(R.id.act_search_listView)
    ListView actSearchListView;
    //    快速索引
    @BindView(R.id.quickIndex)
    QuickIndexBar quickIndex;
    @BindView(R.id.tv_tips)
    TextView tv_tips;
    private List<City> list;
    private TextView actSearchLocationText;
    //    //    最近搜索
//    private GridView zuijin;
    //    热门搜索
    GridView remen;
    private String[] citys;
    private int[][] states;
    private int headerViewsCount;
    private AutoLinearLayout sec;

    @Override
    protected void init() {
        super.init();
//        获取所有城市信息
        list = new ArrayList<>();
        String city_json = "";
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        String str = sp.getString(SpKey.AAASSEST, "");
        if (!TextUtils.isEmpty(str))
            city_json = str;
        else {
            city_json = FileUtil.readAssets(UIUtils.getContext(), "aa.json");
            sp.edit().putString(SpKey.AAASSEST, city_json).commit();
        }
        try {
            JSONObject jsonObjec = new JSONObject(city_json);
            JSONArray jsonArray = jsonObjec.optJSONArray("RECORDS");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.optJSONObject(i);
                String shortname = jsonObject.optString("areaname");
                String id = jsonObject.optString("id");
                City city = new City(shortname, id);
                list.add(city);
            }
            Collections.sort(list);
        } catch (JSONException e) {

        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_location);
        ButterKnife.bind(this);
        addHeadView();
        if (list.size() > 0) {
            actSearchListView.setAdapter(new CityAdapter(this, list));
        }
    }

    /**
     * 添加头布局
     */
    private void addHeadView() {
        View headView = UIUtils.inflate(R.layout.activity_loc_headview);
        actSearchLocationText = (TextView) headView.findViewById(R.id.act_search_location_text);
        sec = (AutoLinearLayout) headView.findViewById(R.id.sec);
//        zuijin = (GridView) headView.findViewById(R.id.zuijin);
        remen = (GridView) headView.findViewById(R.id.remen);
        actSearchListView.addHeaderView(headView);
    }

    @Override
    protected void initData() {
        super.initData();
        states = new int[2][];
        states[0] = new int[]{android.R.attr.state_pressed};
        states[1] = new int[]{};
        citys = new String[]{"上海", "北京", "广州"};
        remen.setAdapter(new ArrayAdapter(this, R.layout.activity_location_zuijin_city, citys));
    }

    @Override
    protected void initListener() {
        super.initListener();
//        最近/热门/所有
//        zuijin.setOnItemClickListener(this);
        String bdloc = new SharedConfig(UIUtils.getContext()).getCommoneSp().getString(SpKey.BDLOC, "");
        if (!TextUtils.isEmpty(bdloc)) {
            actSearchLocationText.setVisibility(View.VISIBLE);
            sec.setVisibility(View.VISIBLE);
            if (bdloc.endsWith("市"))
                actSearchLocationText.setText(bdloc.substring(0, bdloc.length() - 1));
            else
                actSearchLocationText.setText(bdloc);
        } else {
            actSearchLocationText.setVisibility(View.GONE);
            sec.setVisibility(View.GONE);
        }
        remen.setOnItemClickListener(this);
        actSearchListView.setOnItemClickListener(this);
//        点击快速索引
        quickIndex.setOnLetterChangeListener(new QuickIndexBar.OnLetterChangeListener() {
            @Override
            public void onLetterChange(String letter) {
                tv_tips.setVisibility(View.VISIBLE);
                tv_tips.setText(letter);
                for (int i = 0; i < list.size(); i++) {
                    City city = list.get(i);
                    String pinyin = city.pinyin;
                    if (TextUtils.equals(pinyin.charAt(0) + "", letter)) {
                        actSearchListView.setSelection(i);
                        break;
                    }
                }
            }

            @Override
            public void onReset() {
                tv_tips.setVisibility(View.GONE);
            }
        });
    }

    /**
     * 各分类显示条目点击事件
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
//            case R.id.zuijin:
//                sendBroad(citys[position]);
//                break;
            case R.id.remen:
                sendBroad(citys[position]);
                break;
            case R.id.act_search_listView:
                if (headerViewsCount == 0)
                    headerViewsCount = actSearchListView.getHeaderViewsCount();
                sendBroad(list.get(position - headerViewsCount).name);
                break;
        }
    }

    Intent intent;

    public void sendBroad(String name) {
        intent = new Intent();
        intent.setAction("loc");
        City cityByName = getCityByName(name);
        intent.putExtra(SpKey.LOC, cityByName);
        sendBroadcast(intent);
        finish();
    }

    public City getCityByName(String name) {
        City currCity = null;
        for (City city : list) {
            if (city.name.contains(name)) {
                currCity = city;
                break;
            }
        }
        return currCity;
    }
}
